﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace HHHFramework {
    /// <summary>
    /// assetbundle
    /// </summary>
    public class AssetBundleRes : Res
    {
        private string mPath;
        public AssetBundle assetBundle{
            get { return Asset as AssetBundle; }
            set { Asset = value; }
        }
        public AssetBundleRes(ResSearchKeys resSearchKeys)
        {
            State = ResState.Waiting;
            mAssetName = resSearchKeys.AssetName;
            AssetType = resSearchKeys.AssetType;
            OwnerBundleName = resSearchKeys.OwnerBundle;

            mPath = ResKitUtil.FullPathForAssetBundle(resSearchKeys.AssetName);
        }
        ResLoad mResLoad = new ResLoad();
        /// <summary>
        /// Load 同步
        /// </summary>
        public override bool LoadSync()
        {
            State = ResState.Loading;
            //var dependencyBundleNames = Manifest.GetDirectDependencies(Name);
            var dependencyBundleNames = ResData.Instance.GetDirectDependencies(mAssetName);
            //if (dependencyBundleNames.Length==0) { Debug.LogError("没有成功加载"+ mAssetName); }
            foreach (var dependencyBundle in dependencyBundleNames)
            {
                mResLoad.LoadSync<AssetBundle>(dependencyBundle);
            }
            if (!ResMgr.IsSimulationModelogic) {
                assetBundle = AssetBundle.LoadFromFile(mPath);
                Debug.Log("assetBundle load    " + mAssetName);
                //Asset = assetBundle.LoadAsset(AssetName);
            }
            State = ResState.Loaded;
            return assetBundle!=null?true:false;
        }
        /// <summary>
        /// 查询所有关于被当前Assetbundle引用的AssetBundle是否被加载  如果没有被加载需要进行加载
        /// </summary>
        /// <param name="onAllLoaded"></param>
        private void LoadDependencyBundlesAsync(Action onAllLoaded) {
            //var dependencyBundleNames = Manifest.GetDirectDependencies(Name);
            var dependencyBundleNames = ResData.Instance.GetDirectDependencies(mAssetName);
            if (dependencyBundleNames.Length ==0) {
                onAllLoaded?.Invoke();
                return;
            }
            int loadedCount = 0;
            foreach (var dependencyBundle in dependencyBundleNames)
            {
                mResLoad.LoadAsync<AssetBundle>(dependencyBundle, dependeBundle=>{
                    loadedCount++;
                    if (loadedCount==dependencyBundleNames.Length) {
                        onAllLoaded();
                    }
                });
            }
        }

        public override void LoadAsync()
        {
            State = ResState.Loading;
            LoadDependencyBundlesAsync(()=> {
                if (ResMgr.IsSimulationModelogic)
                {
                    State = ResState.Loaded;
                }
                else {

                    var request = AssetBundle.LoadFromFileAsync(mPath);
                    request.completed += operation =>
                    {
                        assetBundle = request.assetBundle;
                        State = ResState.Loaded;
                    };
                }

            });

            
        }

        protected override void OnReleaseRes()
        {
            if (assetBundle!=null) { assetBundle.Unload(true);}          
            ResMgr.Instance.sharedLoadRes.Remove(this);
            mResLoad.ReleaseAll();
            mResLoad = null;
            assetBundle = null;
        }         
    }
}

